<template>
    <div>
        <van-nav-bar title="标题" left-text="返回" right-text="按钮" left-arrow @click-left="onClickLeft" />
        <van-address-edit :area-list="areaList" show-postal show-delete show-set-default show-search-result
            :search-result="searchResult" :area-columns-placeholder="['请选择', '请选择', '请选择']" @save="onSave"
            @delete="onDelete" @change-detail="onChangeDetail"  />
    </div>
</template>

<script>
import { areaList } from '@vant/area-data';
import axios from "axios";
import { Toast } from 'vant';
export default {
    data() {
        return {
            areaList,
            searchResult: [],

        };
    },
    methods: {
        onSave(sss) {
            Toast('save');
            console.log(sss);
            if (sss.isDefault = false) {
                axios({
                    url: "/api/user/insertRece",
                    method: "POST",
                    params: {
                        receName: sss.name,
                        recePhone: sss.tel,
                        receProvince: sss.province,
                        receCity: sss.city,
                        receCounty: sss.county,
                        receCode: sss.areaCode,
                        receDetail: sss.addressDetail,
                        isDefault: 1,
                    }
                }).then((res) => {
                    Toast.success('添加成功');
                });
            } else if (sss.isDefault = true) {
                axios({
                    url: "/api/user/updateDefa",
                    method: "get",
                }).then((res) => {
                   this.insertList(sss);
                });
            }

        },
        onDelete() {
            Toast('delete');
        },
        insertList(sss){
            axios({
                    url: "/api/user/insertRece",
                    method: "POST",
                    params: {
                        receName: sss.name,
                        recePhone: sss.tel,
                        receProvince: sss.province,
                        receCity: sss.city,
                        receCounty: sss.county,
                        receCode: sss.areaCode,
                        receDetail: sss.addressDetail,
                        isDefault: 2,
                    }
                }).then((res) => {
                    Toast.success('添加成功');
                });
        },
        onChangeDetail(val) {
            if (val) {
                this.searchResult = [
                    {
                        name: '黄龙万科中心',
                        address: '杭州市西湖区',
                    },
                ];
            } else {
                this.searchResult = [];
            }
        },
        onClickLeft() {
            this.$router.push({ path: '/addressList' })
        },
    },
    created() {
    },

}
</script>

<style>

</style>